package pe.com.chilis.delivery.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.com.chilis.delivery.beans.BCliente;
import pe.com.chilis.delivery.log.UtilLog;
import pe.com.chilis.delivery.negocio.NCliente;

/**
 * Servlet implementation class SCliente
 */
public class SCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SCliente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesarRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesarRequest(request, response);
	}

	private void procesarRequest(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String accion = request.getParameter("accion");
		String pagina = "";
		try {
			if("0".equals(accion)){
				pagina = buscarClientePorNombre(request, response);
			}else if("1".equals(accion)){
				pagina = seleccionaCliente(request, response);
			}
			
			response.sendRedirect(pagina);
			
		} catch (Exception e) {
			UtilLog.imprimeExcepcion(e);
		}
	}

	@SuppressWarnings("unchecked")
	private String seleccionaCliente(HttpServletRequest request,
			HttpServletResponse response) {
		String strPagina = "pages/registrarPedido.jsp";
		try {
			
			String codigo = request.getParameter("codigo");
			
			ArrayList<BCliente> lstCliente = (ArrayList<BCliente>)request.getSession().getAttribute("lstCliente");
			BCliente objCliente = new BCliente();
			for(BCliente cliente : lstCliente){
				if(cliente.getIdCliente().equals(codigo)){
					objCliente = cliente;
					break;
				}
			}
			objCliente.setDireccion("Av. Los Alamos 1520");
			objCliente.setIdTipoDocumentoIdentidad("DNI");
			request.getSession().setAttribute("objCliente", objCliente);
			
		} catch (Exception e) {
			UtilLog.imprimeExcepcion(e);
		}
		return strPagina;
	}

	private String buscarClientePorNombre(HttpServletRequest request, HttpServletResponse response) {
		String strPagina = "pages/inc/inc_buscarCliente.jsp";
		try {
			
			String nombre = request.getParameter("nombre").toUpperCase();
			String apellidoPat = request.getParameter("apellidoPat").toUpperCase();
			String apellidoMat = request.getParameter("apellidoMat").toUpperCase();
			
			BCliente objCliente = new BCliente();
			objCliente.setNombres(nombre);
			objCliente.setApellidoPaterno(apellidoPat);
			objCliente.setApellidoMaterno(apellidoMat);
			
			ArrayList<BCliente> lstCliente = NCliente.buscarClientesPorNombre(objCliente);
			
			request.getSession().setAttribute("lstCliente", lstCliente);
			
		} catch (Exception e) {
			UtilLog.imprimeExcepcion(e);
		}
		return strPagina;
	}

}
